/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Clases;

import Entities.Cheques;
import Frames.PANEL;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;


/**
 *
 * @author Romi
 */
public class Reportes {
    
    MFecha Fecha = new MFecha();
    String PathExt = new File("Ext").getAbsolutePath();
    String PathInformes = new File("informes").getAbsolutePath();
    String LogoInformes="LogoInformes.png";
    String Logo="Logo.png";
    
    Controller.ChequesJpaController JPACheque = new Controller.ChequesJpaController(PANEL.EM);
    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Reportes.class);
    
    
    private Connection GetConnection(){
        EntityManager em = PANEL.EM.createEntityManager() ;
        em.getTransaction().begin();
        java.sql.Connection connection = em.unwrap(java.sql.Connection.class);
        em.getTransaction().commit();
        return connection;
    }
      
    
    public void ImprimirListadoCheques(){        
        InputStream Logo=null;
        List<Cheques> lista = new ArrayList<>();
        try {
            Logo = new FileInputStream(PathExt+File.separator+LogoInformes);
            lista = JPACheque.Listar();
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
        String reporte = PathInformes+File.separator+"Cheques.jasper";
        JasperPrint re=null;            
        Map parametros = new HashMap();
        parametros.clear(); 
        parametros.put("Logo", Logo); 
        parametros.put("Lista", lista); 
        //parametros.put("SUBREPORT_DIR", PathInformes+File.separator); 
         
        try { 
            re = JasperFillManager.fillReport(reporte, parametros,this.GetConnection());
        } catch (JRException ex) {
            log.error(ex.getMessage());
        }
        re.setName("Orden de Trabajo N°: ");
        JasperViewer viewer = new JasperViewer(re, false);
        viewer.setTitle("Orden de Trabajo N°: ");
        viewer.toFront();
        viewer.show();            
    }
    
}
